//
//  JQFYScrollView.m
//  JQFY
//
//  Created by apple on 2017/8/8.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYScrollView.h"
#import "JQFYSong.h"

@interface JQFYScrollView()<UIScrollViewDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, retain)NSTimer* rotateTimer;  //让视图自动切换
@property (nonatomic, retain)UIPageControl *myPageControl;
@end

static int count = 6;
@implementation JQFYScrollView{
    NSArray *_dataArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupScrollView];
    }
    return self;
}

-(void)setupScrollView{
    //初始化scrollView 大小
    UIScrollView *rotateScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    //设置滚动范围
    rotateScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.frame)*count, CGRectGetHeight(self.frame));
    //设置分页效果
    rotateScrollView.pagingEnabled = YES;
    //水平滚动条隐藏
    rotateScrollView.showsHorizontalScrollIndicator = NO;
    //添加三个子视图
    for (int i = 0; i< count; i++) {
        UIImageView *subImgView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.frame)*i, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
        subImgView.tag = 1000 + i;
        NSString* imgName = [NSString stringWithFormat:@"轮播图%d",i+1];
        [subImgView setImage:[UIImage imageNamed:imgName]];
        [rotateScrollView addSubview:subImgView];
    }
    UIImageView *tempImgView = [rotateScrollView viewWithTag:1000];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.frame)*count, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    imgView.image = tempImgView.image;
    [rotateScrollView addSubview:imgView];
    [self addSubview:rotateScrollView];
    rotateScrollView.tag = 1000;
    self.myPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-50, CGRectGetWidth(self.frame), 50)];
    self.myPageControl.numberOfPages = count;
    self.myPageControl.currentPage = 0;
    [self addSubview:self.myPageControl];
    
    //启动定时器
    self.rotateTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeView) userInfo:nil repeats:YES];
    //为滚动视图指定代理
    rotateScrollView.delegate = self;
}

#pragma mark -- 滚动视图的代理方法
//开始拖拽的代理方法，在此方法中暂停定时器。
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"正在拖拽视图，所以需要将自动播放暂停掉");
    //setFireDate：设置定时器在什么时间启动
    //[NSDate distantFuture]:将来的某一时刻
    [self.rotateTimer setFireDate:[NSDate distantFuture]];
}

//视图静止时（没有人在拖拽），开启定时器，让自动轮播
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //视图静止之后，过1.5秒在开启定时器
    //[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]]  返回值为从现在时刻开始 再过1.5秒的时刻。
    NSLog(@"开启定时器");
    [self.rotateTimer setFireDate:[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]]];
}

//定时器的回调方法   切换界面
- (void)changeView{
    //得到scrollView
    UIScrollView *scrollView = [self viewWithTag:1000];
    //通过改变contentOffset来切换滚动视图的子界面
    float offset_X = scrollView.contentOffset.x;
    //每次切换一个屏幕
    offset_X += CGRectGetWidth(self.frame);
    //说明要从最右边的多余视图开始滚动了，最右边的多余视图实际上就是第一个视图。所以偏移量需要更改为第一个视图的偏移量。
    if (offset_X > CGRectGetWidth(self.frame)*_dataArray.count) {
        scrollView.contentOffset = CGPointMake(0, 0);
    }
    //说明正在显示的就是最右边的多余视图，最右边的多余视图实际上就是第一个视图。所以pageControl的小白点需要在第一个视图的位置。
    if (offset_X == CGRectGetWidth(self.frame)*_dataArray.count) {
        self.myPageControl.currentPage = 0;
    }else{
        self.myPageControl.currentPage = offset_X/CGRectGetWidth(self.frame);
    }
    //得到最终的偏移量
    CGPoint resultPoint = CGPointMake(offset_X, 0);
    //切换视图时带动画效果
    //最右边的多余视图实际上就是第一个视图，现在是要从第一个视图向第二个视图偏移，所以偏移量为一个屏幕宽度
    if (offset_X >CGRectGetWidth(self.frame)*_dataArray.count) {
        self.myPageControl.currentPage = 1;
        [scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.frame), 0) animated:YES];
    }else{
        [scrollView setContentOffset:resultPoint animated:YES];
    }
}
@end
